package com.ltmall.api.manager.impl;

import com.ltmall.api.bean.*;
import com.ltmall.api.dao.impl.OrderDAO;
import com.ltmall.api.entity.JsonOrder;
import com.ltmall.api.manager.OrderManager;
import com.ltmall.api.util.PageUtil;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

public class OrderManagerImpl implements OrderManager {
    private OrderDAO oDAO;

    public OrderDAO getoDAO() {
        return oDAO;
    }

    public void setoDAO(OrderDAO oDAO) {
        this.oDAO = oDAO;
    }

    public boolean addOrder(Order order) {
        // TODO Auto-generated method stub
        return oDAO.persist(order);
    }

    public boolean addOrderContent(OrderContent content) {
        // TODO Auto-generated method stub
        return oDAO.addOrderContent(content);
    }

    public boolean delOrder(int orderID) {
        // TODO Auto-generated method stub
        boolean orderTag = oDAO.delOrder(orderID);
        boolean orderContentTag = oDAO.delOrderContent(orderID);
        if (orderTag && orderContentTag) {
            return true;
        }
        return false;
    }

    public List<Order> orderList(int userID, int station, PageUtil page) {
        // TODO Auto-generated method stub
        return oDAO.orderList(userID, station, page);
    }

    public boolean updateOrderStation(int orderID, int station) {
        // TODO Auto-generated method stub
        return oDAO.updateOrderStation(orderID, station);
    }

    public List<OrderContent> getOrderContent(int orderID) {
        // TODO Auto-generated method stub
        return oDAO.getOrderContent(orderID);
    }

    public boolean addOrderLogistic(OrderLogistic oLogistic) {
        // TODO Auto-generated method stub
        return oDAO.addOrderLogistic(oLogistic);
    }

    public boolean updateOrderFee(int orderID, double fee) {
        return oDAO.updateOrderFee(orderID, fee);
    }

    public boolean updateOrderFeeDirector(int orderID, double fee) {
        return oDAO.updateOrderFeeDirector(orderID, fee);
    }

    public Order getOrder(int orderID) {
        // TODO Auto-generated method stub
        return oDAO.getOrder(orderID);
    }

    public boolean orderUpdate(float wxFee, float zfbFee, float coupopnFee, float giftcardFee,
                               String message, int station, int orderID, int transactionType,
                               String invoice, String congratulations, String deliveryTime) {
        // TODO Auto-generated method stub

        Order order = getOrder(orderID);
        if ((order.getInvoice() != null) && (!order.getInvoice().equals(""))) {
            invoice = order.getInvoice();
        }
        if ((order.getCongratulations() != null) && (!order.getCongratulations().endsWith(""))) {
            congratulations = order.getCongratulations();
        }
        if ((order.getMessage() != null) && (!order.getMessage().equals(""))) {
            message = order.getMessage();
        }

        return oDAO.orderUpdate(wxFee, zfbFee, coupopnFee, giftcardFee, message, station, orderID,
            transactionType, invoice, congratulations, deliveryTime);
    }

    public boolean addComment(Comment comment) {
        // TODO Auto-generated method stub
        return oDAO.addComment(comment);
    }

    public OrderLogistic getOrderLogistic(int orderID) {
        // TODO Auto-generated method stub
        return oDAO.getOrderLogistic(orderID);
    }

    public boolean isComment(int orderID, int goodsID, String goodsFormat, int userID) {
        // TODO Auto-generated method stub
        return oDAO.isComment(userID, orderID, goodsID, goodsFormat);
    }

    public boolean addRefund(Refund refund) {
        // TODO Auto-generated method stub
        return oDAO.addRefund(refund);
    }

    public List<Refund> refundList(int userID, PageUtil page) {
        // TODO Auto-generated method stub
        return oDAO.refundList(userID, page);
    }

    public ActivityPrice getActivityPrice(int goodsID, String format) {
        // TODO Auto-generated method stub
        return oDAO.getActivityPrice(goodsID, format);
    }

    public boolean addTradeNo(TradeNo no) {
        // TODO Auto-generated method stub
        return oDAO.addTradeNo(no);
    }

    public TradeNo getTradeNo(String no) {
        // TODO Auto-generated method stub
        return oDAO.getTradeNo(no);
    }

    public boolean tradeNoUpdate(TradeNo no) {
        // TODO Auto-generated method stub
        return oDAO.tradeNoUpdate(no);
    }

    public boolean addOrderTime(OrderTime orderTime) {
        // TODO Auto-generated method stub
        return oDAO.addOrderTime(orderTime);
    }

    public boolean updateOrderTime(int orderID, int type, long time) {
        // TODO Auto-generated method stub
        return oDAO.updateOrderTime(orderID, type, time);
    }

    public OrderTime getOrderTime(int orderID) {
        return oDAO.getOrderTime(orderID);
    }

    public boolean addShopClienit(ShopClient shopClient) {
        // TODO Auto-generated method stub
        if (!oDAO.shopClientExist(shopClient.getShopID(), shopClient.getUserID())) {
            return oDAO.addShopClient(shopClient);
        }
        return true;
    }

    public boolean updateOrderStationAndTransaction(int orderID, int station, int transType,
                                                    String transNumber) {
        // TODO Auto-generated method stub
        return oDAO.updateOrderStationAndTransaction(orderID, station, transType, transNumber);
    }

    public boolean deleteCouponCount(int userID, float fee) {
        // TODO Auto-generated method stub
        return oDAO.deleteCouponCount(userID, fee);
    }

    public boolean updateActivityGoodsSellout(int activityID, int goodsID, String goodsFormat,
                                              int number) {
        // TODO Auto-generated method stub
        return oDAO.updateActivityGoodsSellout(activityID, goodsID, goodsFormat, number);
    }

    public boolean updateActivityGoodsSellout(int activityID, int skuID, int number) {
        return oDAO.updateActivityGoodsSellout(activityID, skuID, number);
    }

    public boolean addShopNews(ShopNews news) {
        // TODO Auto-generated method stub
        return oDAO.addShopNews(news);
    }

    public ShopNews getShopNews(int shopID, int userID) {
        // TODO Auto-generated method stub
        return oDAO.getShopNews(shopID, userID);
    }

    public boolean cancelOrder(int orderID) {
        // TODO Auto-generated method stub
        List<OrderContent> contentList = oDAO.getOrderContent(orderID);
        boolean groupTag = true;
        if (contentList.size() > 0) {
            OrderContent content = contentList.get(0);
            if (content.getActivityName().equals("ThGroupBuyingRules") && content.getGroupID() > 0
                && content.getActivitySuccess() == 0) {
                //团购活动现在不支持取消订单
                //groupTag = oDAO.groupDel(content.getGroupID());
            }
        }
        return oDAO.cancelOrder(orderID);
    }

    public boolean addSellout(SelloutRecord record) {
        // TODO Auto-generated method stub
        //		if(oDAO.selloutRecordExist(record.getGoodsID())){
        //			//记录存在修改
        //			return oDAO.updateSelloutRecord(record.getGoodsID(), record.getNumber(), record.getFee());
        //		}else{
        //记录不存在添加
        return oDAO.addSellout(record);
        //		}
    }

    public int orderNumber(int userID, int station) {
        // TODO Auto-generated method stub
        return oDAO.orderNumber(userID, station);
    }

    public int unFinishRefund(int userID) {
        // TODO Auto-generated method stub
        return oDAO.unFinishedRefund(userID);
    }

    public Refund getRefund(int orderID) {
        // TODO Auto-generated method stub
        return oDAO.getRefund(orderID);
    }

    public List<String> distinctNumberInCart(String[] cartIDs) {
        // TODO Auto-generated method stub
        return oDAO.distinctNumberInCart(cartIDs);
    }

    public int goodsNumberInCart(int goodsID, String[] cartIDs) {
        // TODO Auto-generated method stub
        return oDAO.goodsNumberInCart(goodsID, cartIDs);
    }

    public OrderContent getActivityOrderContent(int goodsID, String goodsFormat, int orderID) {
        // TODO Auto-generated method stub
        return oDAO.getActivityOrderContent(goodsID, goodsFormat, orderID);
    }

    public List<Order> activityOrder(long startTime, long endTime, int userID) {
        // TODO Auto-generated method stub
        return oDAO.activityOrder(startTime, endTime, userID);
    }

    public boolean updateActivityGoodsNumber(int activityID, int goodsID, String goodsFormat,
                                             int number) {
        // TODO Auto-generated method stub
        return oDAO.updateActivityGoodsNumber(activityID, goodsID, goodsFormat, number);
    }

    public int getStoreHouse(int shopID) {
        // TODO Auto-generated method stub
        return oDAO.getStoreHouse(shopID);
    }

    public int getShopID(int bussinessID) {
        // TODO Auto-generated method stub
        return oDAO.getShopID(bussinessID);
    }

    public int groupOrderNumber(int userID, int groupRuleId) {
        // TODO Auto-generated method stub
        List<BigInteger> list = new ArrayList<BigInteger>();
        list = oDAO.groupOrderGet(userID, groupRuleId);
        return list.size();
    }

    public ThGroupBuying groupAdd(OrderContent order, ThGroupBuyingRules rule, int userID) {
        // TODO Auto-generated method stub
        return oDAO.groupAdd(order, rule, userID);
    }

    public boolean decreaseStocks(ThGroupBuyingRules rule, int num) {
        // TODO Auto-generated method stub
        return oDAO.decreaseStocks(rule, num);
    }

    public boolean rollbackStocks(int rulesID, int num) {
        return oDAO.rollbackStocks(rulesID, num);
    }

    public boolean orderContentGroupIdAdd(OrderContent order) {
        // TODO Auto-generated method stub
        return oDAO.orderContentGroupIdAdd(order);
    }

    public ThGroupBuying groupUpdate(ThGroupBuying group) {
        // TODO Auto-generated method stub
        return oDAO.groupUpdate(group);
    }

    public boolean orderContentUpdate(OrderContent content) {
        // TODO Auto-generated method stub
        return oDAO.orderContentUpdate(content);
    }

    public boolean rollbackPanicStocks(int panicID, int goodsID, String format, int num) {
        // TODO Auto-generated method stub
        return oDAO.rollbackPanicStocks(panicID, goodsID, format, num);
    }

    public boolean decreasePanicStocks(int panicID, int goodsID, String format, int num) {
        // TODO Auto-generated method stub
        return oDAO.decreasePanicStocks(panicID, goodsID, format, num);
    }

    public int groupOrderGoodsIdGet(int userID, int groupRulesId) {
        // TODO Auto-generated method stub
        List<BigInteger> list = new ArrayList<BigInteger>();
        list = oDAO.groupOrderGet(userID, groupRulesId);
        if (list.size() > 0) {
            String goodsId = list.get(0).toString();
            return Integer.parseInt(goodsId);
        }
        return 0;
    }

    public boolean increaseStocks(int groupRuleId, int num) {
        // TODO Auto-generated method stub
        return oDAO.increaseSellout(groupRuleId, num);
    }

    public BigDecimal orderSubUnderwritingTotalFee(int orderId) {
        return oDAO.orderSubUnderwritingTotalFee(orderId);
    }

    public boolean isPriceSame(final String price, final String skuId) {
        return oDAO.isPriceSame(price, skuId);
    }

    public ActivityPrice getActivityPriceBySkuID(int skuID) {
        return oDAO.getActivityPriceBySkuID(skuID);
    }

    public List<JsonOrder> ordersadd(List<Cart> cartList, int userID, int addressID, int selefAddID) {
        return oDAO.ordersadd(cartList, userID, addressID,selefAddID);
    }

    public boolean reduceActivityNumber(int activityID, int skuID, int number) {
        return oDAO.reduceActivityNumber(activityID, skuID, number);
    }

    public List<Order> diretorPrivilegeOrderList(int userID) {
        return oDAO.diretorPrivilegeOrderList(userID);
    }
}
